package com.sunfeng.netty.atguigu.codec3;

import io.netty.channel.*;
import lombok.extern.slf4j.Slf4j;

/**
 * @author sunfeng
 * @date 2021/05/19 14:38
 **/
@Slf4j
@ChannelHandler.Sharable
public class SubReqServerHandler extends SimpleChannelInboundHandler<SubScribeReq> {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("channelActive");
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, SubScribeReq subScribeReq) throws Exception {
        //SubScribeReq subScribeReq = (SubScribeReq) object;
        if ("SunFeng".equalsIgnoreCase(subScribeReq.getUserName())) {
            System.out.println("服务端接收到客户端数据【" + subScribeReq.toString() + "】");
            ctx.writeAndFlush(resp(subScribeReq.getSubReqID()));
        }

    }

    private SubscribeResp resp(int subReqID) {
        SubscribeResp subscribeResp = new SubscribeResp();
        subscribeResp.setSubReqID(subReqID);
        subscribeResp.setRespCode(0);
        subscribeResp.setDesc("Netty 成功了~~~ 三天后,发货到你这个地址");
        return subscribeResp;
    }
}
